package cn.midas.org.service.impl;

import cn.midas.basic.dto.LoginDto;
import cn.midas.basic.exception.BusinessException;
import cn.midas.basic.service.impl.LoginServiceImpl;
import cn.midas.basic.utils.BaiduAuditUtils;
import cn.midas.org.domain.Employee;
import cn.midas.org.domain.Shop;
import cn.midas.org.domain.ShopAuditLog;
import cn.midas.org.mapper.EmployeeMapper;
import cn.midas.org.mapper.ShopAuditLogMapper;
import cn.midas.org.mapper.ShopMapper;
import cn.midas.org.service.IEmployeeService;
import cn.midas.org.service.IShopService;
import cn.midas.basic.service.impl.BaseServiceImpl;
import cn.midas.org.vo.ShopEchartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itsource
 * @since 2022-07-01
 */
@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;

    @Autowired
    private JavaMailSender javaMailSender;


    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private LoginServiceImpl loginService;
    /**
    * @Title: settlement
    * @Description: 店铺入驻方法
    * @Author: Mr.She
    * @Version: 1.0
    * @Date:  2022/7/1 14:10
    * @Parameters: [shop]
    * @Return void
    */
    @Override
    public void settlement(Shop shop) {
        Employee admin = shop.getAdmin();
        // 1.判断必要参数是否为空，这里我们应该要全部参数都判断是否为空，我就以名称举例
        if (StringUtils.isEmpty(shop.getName())){
            throw new BusinessException("参数不能为空，请重新输入！");
        }
        // 2.两次密码是否一致
        if (!admin.getPassword().equals(admin.getComfirmPassword())){
            throw new BusinessException("两次密码不一致，请重新输入！");
        }

        // 调用百度AI智能审核文本工具类，审核用户的店铺名称
        if (!BaiduAuditUtils.TextCensor(shop.getName())){
            throw new BusinessException("请规范输入店铺名称！");
        }
        // 调用百度AI智能审核文本工具类，审核用户店铺log是否合规
        if (!BaiduAuditUtils.ImgCensor(shop.getLogo())){
            throw new BusinessException("请规范上传log图片！");
        }

        // 3.查询店铺是否存在,根据店铺名称查询
        Shop loadShopTmp = shopMapper.loadByName(shop.getName());
        if (loadShopTmp != null){
            throw new BusinessException("店铺已注册，请更换店铺信息！");
        }
        // 4.查询管理员已经注册过，并且还绑定了某个店铺，这种情况我们认为你不符合我们业务要求，抛出异常
        Employee empTmp = employeeMapper.loadByUserName(admin.getUsername());
        // 此处你们可以自己更改为根据主键查询店铺的admin_id
        if (empTmp != null && empTmp.getShopId() != null){
            throw new BusinessException("该管理员已绑定店铺，请更换其他管理员！");
        }
        // 5.管理员没注册，那么注册管理员
        if (empTmp == null){
            // 保存admin，一定要保证返回了主键Id
            employeeService.save(admin);
            empTmp = admin;
        }

        // 6.如果走到这里，那么管理员绝对已经注册过了
        // 6.1.将管理员的主键设置到店铺的外键
        shop.setAdminId(empTmp.getId());
        // 6.2.保存店铺,一定要返回主键
        shopMapper.save(shop);
        // 6.3.将店铺的主键设置到管理员的外键
        empTmp.setShopId(shop.getId());
        // 6.4.修改员工数据
        employeeMapper.update(empTmp);
    }

    /**
    * @Title: reject
    * @Description: 驳回店铺
    * @Author: Mr.She
    * @Version: 1.0
    * @Date:  2022/7/2 10:30
    * @Parameters: [shopAuditLog]
    * @Return void
    */
    @Override
    public void reject(ShopAuditLog shopAuditLog) {
        // 1.修改店铺的状态为驳回 自行实现
        Long shopId = Long.valueOf(shopAuditLog.getShopId());
        Shop shop = shopMapper.loadById(shopId);
        shop.setState(-1);
        shopMapper.update(shop);
        // 2.保存日志信息
        Integer integer = Integer.valueOf(Math.toIntExact(loginService.id));
        shopAuditLog.setAuditId(integer);
        shopAuditLogMapper.save(shopAuditLog);
        // 3.发送修改店铺信息邮件给用户
        // 2.发送复杂邮件的工具类
        try {
            // 1.创建复杂邮件对象
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
            helper.setFrom("3108281293@qq.com");
            helper.setSubject("宠物乐园店铺入驻结果通知");
            // 3.设置文本内容，第二个参数 true 代表是否是一个html内容
            helper.setText("<h1>亲，您的店铺入驻申请信息我平台已审核，但是因为内容不合规被驳回，请修改店铺信息后，再次申请</h1>"+
                    "店铺信息修改链接：<a href='http://localhost:8081/#/Register2?shopId="+shopAuditLog.getShopId()+"'>点我修改店铺信息</a>",true);
            // 5.设置收件人
            helper.setTo("3108281293@qq.com");
            // 6.发送邮件
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    /**
    * @Title: pass
    * @Description: 通过店铺
    * @Author: Mr.She
    * @Version: 1.0
    * @Date:  2022/7/2 10:30
    * @Parameters: [shopAuditLog]
    * @Return void
    */
    @Override
    public void pass(ShopAuditLog shopAuditLog) {
        // 1.修改店铺的状态为通过 自行实现
        Long shopId = Long.valueOf(shopAuditLog.getShopId());
        Shop shop = shopMapper.loadById(shopId);
        shop.setState(1);
        shopMapper.update(shop);
        // 2.保存日志信息
        shopAuditLog.setAuditId(325);
        shopAuditLogMapper.save(shopAuditLog);
        // 3.发送激活邮件给用户
        // 2.发送复杂邮件的工具类
        try {
            // 1.创建复杂邮件对象
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
            helper.setFrom("3108281293@qq.com");
            helper.setSubject("宠物乐园店铺入驻结果通知");
            // 3.设置文本内容，第二个参数 true 代表是否是一个html内容
            helper.setText("<h1>亲，您的店铺入驻申请信息我平台已审核通过，请点击下方链接激活店铺</h1>"+
                    "店铺激活链接：<a href='http://localhost:8080/shop/"+shopAuditLog.getShopId()+"'>点我激活店铺</a>",true);
            // 5.设置收件人
            helper.setTo("3108281293@qq.com");
            // 6.发送邮件
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    @Override
    public Map<String, Object> loadShopEcharts() {
        List<ShopEchartVo> shopEchartVos = shopMapper.loadShopEcharts();
       
       // List<Integer> state1 = new ArrayList<>();
        List<String> state = new ArrayList<>();

        List<Integer> countNum = new ArrayList<>();
        for (ShopEchartVo shopEchartVo : shopEchartVos) {
            if (shopEchartVo.getState()==-2){
                String integer1 = "审核拒绝";
                state.add(integer1);
            }else if (shopEchartVo.getState()==-1){
                String integer2 = "审核驳回";
                state.add(integer2);
            }else if (shopEchartVo.getState()==0){
                String integer3 = "待审核";
                state.add(integer3);
            }else {
                String integer4 = "审核通过";
                state.add(integer4);
            }
           // state1.add(shopEchartVo.getState());
            countNum.add(shopEchartVo.getCountNum());
        }
        /*String integer1 = "审核拒绝";
        String integer2 = "审核驳回";
        String integer3 = "待审核";
        String integer4 = "审核通过";
        state.add(integer1);
        state.add(integer2);
        state.add(integer3);
        state.add(integer4);*/
        HashMap<String, Object> map = new HashMap<>();
            map.put("x",state);
            map.put("y",countNum);
            return map;
    }

    /*/**
    * @Title: decline
    * @Description: 拒绝通过
    * @Author: Mr.She
    * @Version: 1.0
    * @Date:  2022/7/3 10:24
    * @Parameters: [shopAuditLog]
    * @Return void
    */
    @Override
    public void decline(ShopAuditLog shopAuditLog) {
        // 1.修改店铺的状态为拒绝 自行实现
        Long shopId = Long.valueOf(shopAuditLog.getShopId());
        Shop shop = shopMapper.loadById(shopId);
        shop.setState(-2);
        shopMapper.update(shop);

        // 2.保存日志信息
        shopAuditLog.setAuditId(325);
        shopAuditLogMapper.save(shopAuditLog);
        // 3.发送激活邮件给用户
        // 2.发送复杂邮件的工具类
        try {
            // 1.创建复杂邮件对象
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
            helper.setFrom("3108281293@qq.com");
            helper.setSubject("宠物乐园店铺入驻结果通知");
            // 3.设置文本内容，第二个参数 true 代表是否是一个html内容
            helper.setText("<h1>亲，您的店铺入驻申请信息我平台拒绝通过，因为信息严重违规</h1>"
                    ,true);
            // 5.设置收件人
            helper.setTo("3108281293@qq.com");
            // 6.发送邮件
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}
